﻿Imports System.Data.OleDb
Public Class editprofile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim sql As String = "SELECT * FROM UserProfile WHERE VisitorId=@f6"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("IslandConnectionString").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f6", User.Identity.Name)
            Dim dr = cmd.ExecuteReader
            dr.Read()
            If Not IsDBNull(dr("FirstName")) Then tb_fn.Text = dr("FirstName")
            If Not IsDBNull(dr("LastName")) Then tb_ln.Text = dr("LastName")
            If Not IsDBNull(dr("Sex")) Then tb_sex.Text = dr("Sex")
            If Not IsDBNull(dr("DateofBirth")) Then
                Dim UserDoB As DateTime = dr("DateofBirth")

                ddl_year.SelectedValue = UserDoB.Year
                ddl_month.SelectedValue = UserDoB.Month
                ddl_day.SelectedValue = UserDoB.Day
            End If
        End If

    End Sub

    Protected Sub btn_submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit.Click
        Dim newFileName As String
        If fup_picture.HasFile Then
            newFileName = Guid.NewGuid().ToString() & getSuffix(fup_picture.FileName)
            If fup_picture.HasFile Then
                fup_picture.SaveAs(Server.MapPath("pictures") & "/" & newFileName)
            End If
        End If
        Dim UserDoB As DateTime = New DateTime(CInt(ddl_year.SelectedValue), CInt(ddl_month.SelectedValue), CInt(ddl_day.SelectedValue))
        Dim sql As String = ""
        If fup_picture.HasFile Then
            sql = "UPDATE UserProfile SET FirstName=@f1, LastName=@f2, Portrait=@f3, DateofBirth=@f4, Sex=@f5 WHERE VisitorId=@f6 "
        Else
            sql = "UPDATE UserProfile SET FirstName=@f1, LastName=@f2, DateofBirth=@f4, Sex=@f5 WHERE VisitorId=@f6 "
        End If
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("IslandConnectionString").ConnectionString)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", tb_fn.Text)
        cmd.Parameters.AddWithValue("@f2", tb_ln.Text)
        If fup_picture.HasFile Then cmd.Parameters.AddWithValue("@f3", "Pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f4", UserDoB.Date)
        cmd.Parameters.AddWithValue("@f5", tb_sex.Text)
        cmd.Parameters.AddWithValue("@f6", User.Identity.Name)
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
    End Sub
    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function
End Class